//
//  JKRTopic.m
//  百思不得姐
//
//  Created by Lucky on 16/3/4.
//  Copyright © 2016年 Lucky. All rights reserved.
//

#import "JKRTopic.h"

@interface JKRTopic()



@end

@implementation JKRTopic

+ (NSDictionary *)mj_replacedKeyFromPropertyName
{
    return @{@"small_image" : @"image0",
             @"large_image" : @"image1",
             @"middle_image" : @"image2"};
}

//+ (NSString *)mj_replacedKeyFromPropertyName121:(NSString *)propertyName
//{
//
//}

- (NSString *)create_time
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *create = [fmt dateFromString:_create_time];
    //
    //    NSDate *now = [NSDate date];
    //
    //    NSDateComponents *cmps = [now jkr_differFrom:create];
    //
    //    NSLog(@"%zd, %zd, %zd, %zd, %zd, %zd", cmps.year, cmps.month, cmps.day, cmps.hour, cmps.minute, cmps.second);
    
    if (create.jkr_isThisYesr) { //今年
        if (create.jkr_isToday) { //今天
            NSDateComponents *cmps = [[NSDate date] jkr_differFrom:create];
            
            if (cmps.hour >= 1) { //大于一小时前
                return [NSString stringWithFormat:@"%zd小时前", cmps.hour];
            } else if (cmps.minute >= 1) { //大于一分钟前
                return [NSString stringWithFormat:@"%zd分钟前", cmps.minute];
            } else { //一分钟内
                return @"刚刚";
            }
        } else if (create.jkr_isYesterday) { //昨天
            fmt.dateFormat = @"昨天 HH:mm:ss";
            return [fmt stringFromDate:create];
        } else { //其它
            fmt.dateFormat = @"MM-dd HH:mm:ss";
            return [fmt stringFromDate:create];
        }
    } else { //非今年
        return _create_time;
    }
}


- (CGFloat)cellHeight
{
    if (!_cellHeight) {
        
        CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 4 * JKRTopicCellMargin, MAXFLOAT);
        CGFloat textH = [self.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height;
        
        _cellHeight = JKRTopicCellTextY + JKRTopicCellBottomBarH + textH + 2 * JKRTopicCellMargin;
        
        if (self.type == JKRTopicTypePicture) {
    
            CGFloat pictureW = maxSize.width;
            CGFloat pictureH = pictureW * self.height / self.width;
            
            if (pictureH >= JKRTopicCellPictureMaxH) {
                pictureH = JKRTopicCellPictureBreakH;
                self.bigPicture = YES;
            }
            
            CGFloat pictureX = JKRTopicCellMargin;
            CGFloat pictureY = JKRTopicCellTextY + textH + JKRTopicCellMargin;
            
            _pictureF = CGRectMake(pictureX, pictureY, pictureW, pictureH);
            
            _cellHeight += pictureH + JKRTopicCellMargin;
            
        } else if (self.type == JKRTopicTypeVoice) {
            CGFloat voiceX = JKRTopicCellMargin;
            CGFloat voiceY = JKRTopicCellTextY + textH + JKRTopicCellMargin;
            CGFloat voiceW = maxSize.width;
            CGFloat voiceH = voiceW * self.height / self.width;
            _voiceF = CGRectMake(voiceX, voiceY, voiceW, voiceH);
            
            _cellHeight += voiceH + JKRTopicCellMargin;
        } else if (self.type == JKRTopicTypeVideo) {
            CGFloat videoX = JKRTopicCellMargin;
            CGFloat videoY = JKRTopicCellTextY + textH + JKRTopicCellMargin;
            CGFloat videoW = maxSize.width;
            CGFloat videoH = videoW * self.height / self.width;
            _videoF = CGRectMake(videoX, videoY, videoW, videoH);
            
            _cellHeight += videoH + JKRTopicCellMargin;
        }
    }
    
    return _cellHeight;
}

@end
